package model;

import com.opensymphony.xwork2.ActionSupport;

import java.sql.ResultSet;
import java.sql.Time;
import java.sql.Timestamp;
import java.util.Date;
import java.util.*;
import java.sql.*;
import java.text.SimpleDateFormat;

import org.apache.struts2.ServletActionContext;


import vo.*;
import vo.mixed.*;
import metalink.BeanGroupDetails;
import metalink.BeanGroupMember;
import metalink.BeanGroupUpdates;
import metalink.BeanGroupUpdatesComplete;
import model.admin.*;
import myutil.*;

import java.io.*;

import javax.servlet.http.HttpSession;

public class GroupsMo {
	
	public ArrayList<BeanGroupDetails> getGroupsMo(int user_id){
		Connect c=new Connect();
		ResultSet rs;
		HttpSession sessioncheck;
		sessioncheck = ServletActionContext.getRequest().getSession();
		//ArrayList<BeanGroupMember> group_members = new ArrayList<BeanGroupMember>();
		ArrayList<BeanGroupDetails> groups = new ArrayList<BeanGroupDetails>();
		try {
			
			if(sessioncheck.getAttribute("user_type").equals("admin") || sessioncheck.getAttribute("user_type").equals("professor"))
			{
				rs = c.getResult("select * From groups g where group_id=(select group_id from groups_member where staff_member_id='"+user_id+"')");
				while(rs.next()){
					int group_id = rs.getInt("group_id");
					//group_members = getGroupMembers(group_id);
					//int member_count = group_members.size();
					BeanGroupDetails bgd = getGroupDetailsMo(group_id);
					//BeanGroupDetails bgd = new BeanGroupDetails(group_id, rs.getInt("owner_id"), member_count, rs.getString("group_name"), rs.getString("create_date"), rs.getString("last_update_date"), group_members);
					groups.add(bgd);
				}
			}
			else if(sessioncheck.getAttribute("user_type").equals("student"))
			{
				rs = c.getResult("select * From groups g where group_id=(select group_id from groups_member where student_member_id='"+user_id+"')");
				while(rs.next()){
					int group_id = rs.getInt("group_id");
					//group_members = getGroupMembers(group_id);
					//int member_count = group_members.size();
					BeanGroupDetails bgd = getGroupDetailsMo(group_id);
					//BeanGroupDetails bgd = new BeanGroupDetails(group_id, rs.getInt("owner_id"), member_count, rs.getString("group_name"), rs.getString("create_date"), rs.getString("last_update_date"), group_members);
					groups.add(bgd);
				}
			}
			return groups;
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return groups;
	}
	
	public ArrayList<BeanGroupMember> getGroupMembersMo(int group_id){
		ArrayList<BeanGroupMember> group_members = new ArrayList<BeanGroupMember>();
		Connect c=new Connect();
		try{
			ResultSet rs1 = c.getResult("select * from student_details where student_id in (select distinct(student_member_id) from groups_member where group_id='"+group_id+"')");
			while(rs1.next()){
				BeanGroupMember bgr = new BeanGroupMember(rs1.getString("student_name"), rs1.getString("email_id"));
				group_members.add(bgr);
			}
			rs1 = c.getResult("select * from college_staff where staff_id in (select distinct(staff_member_id) from groups_member where group_id='"+group_id+"')");
			while(rs1.next()){
				BeanGroupMember bgr = new BeanGroupMember(rs1.getString("staff_name"), rs1.getString("email_id"));
				group_members.add(bgr);
			}
			return group_members;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return group_members;
	}
	
	public int getUserIdMo(String username){
		Connect c=new Connect();
		ResultSet rs;
		HttpSession sessioncheck;
		sessioncheck = ServletActionContext.getRequest().getSession();
		int user_id = 0;
		try {
			if(sessioncheck.getAttribute("user_type").equals("admin") || sessioncheck.getAttribute("user_type").equals("professor"))
				{
				rs = c.getResult("select staff_id from college_staff where email_id='"+username+"'");
				while(rs.next()){
					user_id = rs.getInt("staff_id");
				}
			}
			else if(sessioncheck.getAttribute("user_type").equals("student"))
			{
				rs = c.getResult("select student_id from student_details where email_id='"+username+"'");
				while(rs.next()){
					user_id = rs.getInt("student_id");
				}
			}
			return user_id;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return user_id;
		
	}
	
	
	public BeanGroupDetails getGroupDetailsMo(int group_id) {
		ArrayList<BeanGroupDetails> groups = new ArrayList<BeanGroupDetails>();
		Connect c=new Connect();
		HttpSession session;
		ResultSet rs;
		BeanGroupDetails bean_group_details;
		String owner_name = null;
		try {
			session = ServletActionContext.getRequest().getSession();
			session.setAttribute("group_id", group_id);
			int grp_id = group_id;
			ArrayList<BeanGroupMember> group_members = new ArrayList<BeanGroupMember>();
			rs = c.getResult("select * From groups g where group_id='"+group_id+"'");
			if(rs.next()){
				group_members = getGroupMembersMo(group_id);
				int member_count = group_members.size();
				int owner_id = rs.getInt("owner_id");
				//groups.add(bgd);
				ResultSet rs1 = c.getResult("select student_name from student_details where student_id='"+owner_id+"'");
				while(rs1.next()){
					owner_name = rs1.getString("student_name");
				}
				bean_group_details = new BeanGroupDetails(group_id, owner_id, owner_name, member_count, rs.getString("group_name"), rs.getString("create_date"), rs.getString("last_update_date"), group_members);
				//groups.add(bean_group_details);
				/*
				group_name = bean_group_details.getGroup_name();
				create_date = bean_group_details.getCreate_date();
				last_update_date = bean_group_details.getLast_update_date();
				*/
				return bean_group_details;
			}
			/*
			ArrayList<BeanGroupUpdatesComplete> all_updates = new ArrayList<BeanGroupUpdatesComplete>();
			all_updates = getGroupUpdates(group_id);
			
			rs = c.getResult("select * from groups_update where group_id='"+group_id+"' and parent_id='0' order by update_time desc");
			while(rs.next()){
				group_updates = new ArrayList<BeanGroupUpdates>();
				int updater_id = rs.getInt("updater_id");
				String updater_role = rs.getString("updater_role");
				if(updater_role.equals("student")){
					ResultSet rs2 = c.getResult("select student_name from student_details where student_id='"+updater_id+"'");
					while (rs2.next()){
						header = new BeanGroupUpdates(rs.getInt("group_update_id"), rs.getInt("group_id"), updater_id, rs.getInt("parent_id"), rs.getInt("document_upload_id"), rs.getInt("status"), rs.getInt("cool_count"), rs.getInt("agree_count"), rs.getString("update"), updater_role, rs.getString("parent_role"), rs.getString("update_time"), rs2.getString("student_name") );
					}
				}
				else {
					ResultSet rs2 = c.getResult("select staff_name from college_staff where staff_id='"+updater_id+"'");
					while(rs2.next()){
						header = new BeanGroupUpdates(rs.getInt("group_update_id"), rs.getInt("group_id"), updater_id, rs.getInt("parent_id"), rs.getInt("document_upload_id"), rs.getInt("status"), rs.getInt("cool_count"), rs.getInt("agree_count"), rs.getString("update"), updater_role, rs.getString("parent_role"), rs.getString("update_time"), rs2.getString("staff_name") );
					}
				}
				System.out.println(header.getGroup_update_id());
				ResultSet rs1 = c.getResult("select * from groups_update where group_id='"+group_id+"' and parent_id='"+header.getGroup_update_id()+"' order by update_time desc");
				while(rs1.next()){
					int c_updater_id = rs1.getInt("updater_id");
					String c_updater_role = rs1.getString("updater_role");
					if(c_updater_role.equals("student")){
						ResultSet rs2 = c.getResult("select student_name from student_details where student_id='"+c_updater_id+"'");
						while (rs2.next()){
							BeanGroupUpdates bgu = new BeanGroupUpdates(rs1.getInt("group_update_id"), rs1.getInt("group_id"), c_updater_id, rs1.getInt("parent_id"), rs1.getInt("document_upload_id"), rs1.getInt("status"), rs1.getInt("cool_count"), rs1.getInt("agree_count"), rs1.getString("update"), c_updater_role, rs1.getString("parent_role"), rs1.getString("update_time"), rs2.getString("student_name") );
							group_updates.add(bgu);
						}
					}
					else {
						ResultSet rs2 = c.getResult("select staff_name from college_staff where staff_id='"+c_updater_id+"'");
						while(rs2.next()){
							BeanGroupUpdates bgu = new BeanGroupUpdates(rs1.getInt("group_update_id"), rs1.getInt("group_id"), c_updater_id, rs1.getInt("parent_id"), rs1.getInt("document_upload_id"), rs1.getInt("status"), rs1.getInt("cool_count"), rs1.getInt("agree_count"), rs1.getString("update"), c_updater_role, rs1.getString("parent_role"), rs1.getString("update_time"), rs2.getString("staff_name") );
							group_updates.add(bgu);
						}
					}
				}
				BeanGroupUpdatesComplete bguc = new BeanGroupUpdatesComplete(header.getGroup_update_id(), header.getGroup_id(), header.getUpdater_id(), header.getParent_id(), header.getDocument_upload_id(), header.getStatus(), header.getUpdate(), header.getUpdater_role(), header.getParent_role(), header.getUpdate_time(), header.getUpdater_name(), group_updates, group_updates.size(), header.getCool_count(), header.getAgree_count());
				all_updates.add(bguc);
				*/
			
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	
	
	public ArrayList<BeanGroupUpdatesComplete> getGroupUpdatesMo(int group_id){
		ArrayList<BeanGroupUpdatesComplete> all_updates = new ArrayList<BeanGroupUpdatesComplete>();
		Connect c = new Connect();
		BeanGroupUpdates header;
		SimpleDateFormat sdf = new SimpleDateFormat("yy-MM-dd hh:mm:ss");
		try{
			ResultSet rs = c.getResult("select * from groups_update where group_id='"+group_id+"' and parent_id='0' and status=1 order by update_time desc");
			while(rs.next()){
				//ArrayList<BeanGroupUpdates> group_updates = new ArrayList<BeanGroupUpdates>();
				ArrayList<BeanGroupUpdates> comment = new ArrayList<BeanGroupUpdates>();
				int updater_id = rs.getInt("updater_id");
				String updater_role = rs.getString("updater_role");
				if(updater_role.equals("student")){
					ResultSet rs2 = c.getResult("select student_name from student_details where student_id='"+updater_id+"'");
					while (rs2.next()){
						Date convertdate = sdf.parse(rs.getString("update_time"));
						header = new BeanGroupUpdates(rs.getInt("group_update_id"), rs.getInt("group_id"), updater_id, rs.getInt("parent_id"), rs.getInt("document_upload_id"), rs.getInt("status"), rs.getInt("cool_count"), rs.getInt("agree_count"), rs.getString("update"), updater_role, rs.getString("parent_role"), convertdate.toString(), rs2.getString("student_name") );
						comment = CommentsMo(header.getGroup_update_id());
						BeanGroupUpdatesComplete bguc = new BeanGroupUpdatesComplete(header.getGroup_update_id(), header.getGroup_id(), header.getUpdater_id(), header.getParent_id(), header.getDocument_upload_id(), header.getStatus(), header.getUpdate(), header.getUpdater_role(), header.getParent_role(), header.getUpdate_time(), header.getUpdater_name(), comment, comment.size(), header.getCool_count(), header.getAgree_count());
						all_updates.add(bguc);
					}
				}
				else {
					ResultSet rs2 = c.getResult("select staff_name from college_staff where staff_id='"+updater_id+"'");
					while(rs2.next()){
						Date convertdate = sdf.parse(rs.getString("update_time"));
						header = new BeanGroupUpdates(rs.getInt("group_update_id"), rs.getInt("group_id"), updater_id, rs.getInt("parent_id"), rs.getInt("document_upload_id"), rs.getInt("status"), rs.getInt("cool_count"), rs.getInt("agree_count"), rs.getString("update"), updater_role, rs.getString("parent_role"), convertdate.toString(), rs2.getString("staff_name") );
						comment = CommentsMo(header.getGroup_update_id());
						BeanGroupUpdatesComplete bguc = new BeanGroupUpdatesComplete(header.getGroup_update_id(), header.getGroup_id(), header.getUpdater_id(), header.getParent_id(), header.getDocument_upload_id(), header.getStatus(), header.getUpdate(), header.getUpdater_role(), header.getParent_role(), header.getUpdate_time(), header.getUpdater_name(), comment, comment.size(), header.getCool_count(), header.getAgree_count());
						all_updates.add(bguc);
					}
				}
				/*
				System.out.println(header.getGroup_update_id());
				ResultSet rs1 = c.getResult("select * from groups_update where group_id='"+group_id+"' and parent_id='"+header.getGroup_update_id()+"' order by update_time desc");
				while(rs1.next()){
					int c_updater_id = rs1.getInt("updater_id");
					String c_updater_role = rs1.getString("updater_role");
					if(c_updater_role.equals("student")){
						ResultSet rs2 = c.getResult("select student_name from student_details where student_id='"+c_updater_id+"'");
						while (rs2.next()){
							BeanGroupUpdates bgu = new BeanGroupUpdates(rs1.getInt("group_update_id"), rs1.getInt("group_id"), c_updater_id, rs1.getInt("parent_id"), rs1.getInt("document_upload_id"), rs1.getInt("status"), rs1.getInt("cool_count"), rs1.getInt("agree_count"), rs1.getString("update"), c_updater_role, rs1.getString("parent_role"), rs1.getString("update_time"), rs2.getString("student_name") );
							group_updates.add(bgu);
						}
					}
					else {
						ResultSet rs2 = c.getResult("select staff_name from college_staff where staff_id='"+c_updater_id+"'");
						while(rs2.next()){
							BeanGroupUpdates bgu = new BeanGroupUpdates(rs1.getInt("group_update_id"), rs1.getInt("group_id"), c_updater_id, rs1.getInt("parent_id"), rs1.getInt("document_upload_id"), rs1.getInt("status"), rs1.getInt("cool_count"), rs1.getInt("agree_count"), rs1.getString("update"), c_updater_role, rs1.getString("parent_role"), rs1.getString("update_time"), rs2.getString("staff_name") );
							group_updates.add(bgu);
						}
					}
				}
				
				BeanGroupUpdatesComplete bguc = new BeanGroupUpdatesComplete(header.getGroup_update_id(), header.getGroup_id(), header.getUpdater_id(), header.getParent_id(), header.getDocument_upload_id(), header.getStatus(), header.getUpdate(), header.getUpdater_role(), header.getParent_role(), header.getUpdate_time(), header.getUpdater_name(), group_updates, group_updates.size(), header.getCool_count(), header.getAgree_count());
				all_updates.add(bguc);
				*/
			}
			
			return all_updates;
		} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
		}
		return all_updates;
	}
		
	
	
	public ArrayList<BeanGroupUpdates> CommentsMo(int group_update_id){
		Connect c = new Connect();
		SimpleDateFormat sdf = new SimpleDateFormat("yy-MM-dd hh:mm:ss");
		ArrayList<BeanGroupUpdates> comment = new ArrayList<BeanGroupUpdates>();
		try{
			
			ResultSet rs11 = c.getResult("select * from groups_update where parent_id='"+group_update_id+"' and status=1 order by update_time desc");
			while(rs11.next()){
				int c_updater_id = rs11.getInt("updater_id");
				String c_updater_role = rs11.getString("updater_role");
				if(c_updater_role.equals("student")){
					ResultSet rs12 = c.getResult("select student_name from student_details where student_id='"+c_updater_id+"'");
					while (rs12.next()){
						Date convertdate = sdf.parse(rs11.getString("update_time"));
						BeanGroupUpdates bgu = new BeanGroupUpdates(rs11.getInt("group_update_id"), rs11.getInt("group_id"), c_updater_id, rs11.getInt("parent_id"), rs11.getInt("document_upload_id"), rs11.getInt("status"), rs11.getInt("cool_count"), rs11.getInt("agree_count"), rs11.getString("update"), c_updater_role, rs11.getString("parent_role"), convertdate.toString(), rs12.getString("student_name") );
						comment.add(bgu);
					}
				}
				else {
					ResultSet rs12 = c.getResult("select staff_name from college_staff where staff_id='"+c_updater_id+"'");
					while(rs12.next()){
						Date convertdate = sdf.parse(rs11.getString("update_time"));
						BeanGroupUpdates bgu = new BeanGroupUpdates(rs11.getInt("group_update_id"), rs11.getInt("group_id"), c_updater_id, rs11.getInt("parent_id"), rs11.getInt("document_upload_id"), rs11.getInt("status"), rs11.getInt("cool_count"), rs11.getInt("agree_count"), rs11.getString("update"), c_updater_role, rs11.getString("parent_role"), convertdate.toString(), rs12.getString("staff_name") );
						comment.add(bgu);
					}
				}
			}
		return comment;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return comment;
	}
	
	public void deletePostMo(int group_update_id){
		Connect c = new Connect();
		try {
			c.dml("update groups_update set status=0 where group_update_id="+group_update_id+"");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public void deleteCommentMo(int group_update_id){
		Connect c = new Connect();
		try {
			c.dml("update groups_update set status=0 where group_update_id="+group_update_id+"");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public ArrayList<BeanGroupUpdates> getUpdateMo(int group_update_id){
		Connect c = new Connect();
		SimpleDateFormat sdf = new SimpleDateFormat("yy-MM-dd hh:mm:ss");
		ArrayList<BeanGroupUpdates> wall_post = new ArrayList<BeanGroupUpdates>();
		try{
			
			ResultSet rs11 = c.getResult("select * from groups_update where group_update_id='"+group_update_id+"' and status=1");
			while(rs11.next()){
				int c_updater_id = rs11.getInt("updater_id");
				String c_updater_role = rs11.getString("updater_role");
				if(c_updater_role.equals("student")){
					ResultSet rs12 = c.getResult("select student_name from student_details where student_id='"+c_updater_id+"'");
					while (rs12.next()){
						Date convertdate = sdf.parse(rs11.getString("update_time"));
						BeanGroupUpdates bgu = new BeanGroupUpdates(rs11.getInt("group_update_id"), rs11.getInt("group_id"), c_updater_id, rs11.getInt("parent_id"), rs11.getInt("document_upload_id"), rs11.getInt("status"), rs11.getInt("cool_count"), rs11.getInt("agree_count"), rs11.getString("update"), c_updater_role, rs11.getString("parent_role"), convertdate.toString(), rs12.getString("student_name") );
						wall_post.add(bgu);
					}
				}
				else {
					ResultSet rs12 = c.getResult("select staff_name from college_staff where staff_id='"+c_updater_id+"'");
					while(rs12.next()){
						Date convertdate = sdf.parse(rs11.getString("update_time"));
						BeanGroupUpdates bgu = new BeanGroupUpdates(rs11.getInt("group_update_id"), rs11.getInt("group_id"), c_updater_id, rs11.getInt("parent_id"), rs11.getInt("document_upload_id"), rs11.getInt("status"), rs11.getInt("cool_count"), rs11.getInt("agree_count"), rs11.getString("update"), c_updater_role, rs11.getString("parent_role"), convertdate.toString(), rs12.getString("staff_name") );
						wall_post.add(bgu);
					}
				}
			}
		return wall_post;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return wall_post;
	}

	public void coolMo(int group_update_id){
		Connect c = new Connect();
		int cool = 0;
		try {
			ResultSet rs10 = c.getResult("select cool_count from groups_update where group_update_id='"+group_update_id+"'");
			while(rs10.next()){
				cool = rs10.getInt("cool_count");
			}
			cool++;
			c.dml("update groups_update set cool_count='"+cool+"' where group_update_id='"+group_update_id+"'");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public void coolCommentMo(int group_update_id){
		Connect c = new Connect();
		int cool = 0;
		try {
			ResultSet rs10 = c.getResult("select cool_count from groups_update where group_update_id='"+group_update_id+"'");
			while(rs10.next()){
				cool = rs10.getInt("cool_count");
			}
			cool++;
			c.dml("update groups_update set cool_count='"+cool+"' where group_update_id='"+group_update_id+"'");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public void agreeMo(int group_update_id){
		Connect c = new Connect();
		int agree=0;
		try {
			ResultSet rs10 = c.getResult("select agree_count from groups_update where group_update_id='"+group_update_id+"'");
			while(rs10.next()){
				agree = rs10.getInt("agree_count");
			}
			agree++;
			c.dml("update groups_update set agree_count='"+agree+"' where group_update_id='"+group_update_id+"'");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public void agreeCommentMo(int group_update_id){
		Connect c = new Connect();
		int agree=0;
		try {
			ResultSet rs10 = c.getResult("select agree_count from groups_update where group_update_id='"+group_update_id+"'");
			while(rs10.next()){
				agree = rs10.getInt("agree_count");
			}
			agree++;
			c.dml("update groups_update set agree_count='"+agree+"' where group_update_id='"+group_update_id+"'");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public void addCommentMo(int group_update_id, String addcomment) {
		Connect c = new Connect();
		HttpSession sessioncheck = ServletActionContext.getRequest().getSession();
		HttpSession session = ServletActionContext.getRequest().getSession();
		String username=ServletActionContext.getRequest().getSession().getAttribute("username").toString();
		int user_id = getUserIdMo(username);
		try{
			//ResultSet rs14;
			Date date = new Date();
			Timestamp timestamp = new Timestamp(date.getTime());
			System.out.println(timestamp);
			c.dml("insert into groups_update( `group_id`, `updater_id`, `parent_id`, `update`, `updater_role`, `status`, `update_time`) values("+session.getAttribute("group_id")+", "+user_id+", "+group_update_id+", '"+addcomment+"', '"+sessioncheck.getAttribute("user_type")+"', 1, '"+timestamp+"')");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public void addMemberMo(String email_id, int group_id){
		Connect c = new Connect();
		HttpSession sessioncheck = ServletActionContext.getRequest().getSession();
		HttpSession session = ServletActionContext.getRequest().getSession();
		String username=ServletActionContext.getRequest().getSession().getAttribute("username").toString();
		int user_id = getUserIdMo(username);
		int member_id=0, flag=0, member_present_check=0;
		ArrayList<BeanGroupMember> members = new ArrayList<BeanGroupMember>();
		try {
			Date date = new Date();
			Timestamp timestamp = new Timestamp(date.getTime());
			
			ResultSet rs = c.getResult("select * from student_details where email_id='"+email_id+"'");
			while(rs.next()){
				member_id = rs.getInt("student_id");
				members = getGroupMembersMo(group_id);
				Iterator<BeanGroupMember> iter;
				iter = members.iterator();
				while(iter.hasNext()){
					if(iter.next().getMember_id().equals(member_id)){
						member_present_check = 1;
					}
				}
				if(member_present_check == 0){
					c.dml("insert into groups_member(`student_member_id`, `group_id`, `added_by_id`, `add_date`) values("+member_id+", "+group_id+", "+user_id+", '"+timestamp+"')");
					flag=1;
				}
			}
			if(flag == 0){
				rs = c.getResult("select * from college_staff where email_id='"+email_id+"'");
				while(rs.next()){
					member_id = rs.getInt("staff_id");
					members = getGroupMembersMo(group_id);
					Iterator<BeanGroupMember> iter;
					iter = members.iterator();
					while(iter.hasNext()){
						if(iter.next().getMember_id().equals(member_id)){
							member_present_check = 1;
						}
					}
					if(member_present_check == 0){
						c.dml("insert into groups_member(`staff_member_id`, `group_id`, `added_by_id`, `add_date`) values("+member_id+", "+group_id+", "+user_id+", '"+timestamp+"')");
						flag=1;
					}
				}
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public void deleteMemberMo(String email_id){
		Connect c = new Connect();
		ResultSet rs;
		int flag=0;
		try {
			rs = c.getResult("select student_id from student_details where email_id='"+email_id+"'");
			while(rs.next()){
				int student_id = rs.getInt("student_id");
				c.dml("delete from groups_member where student_member_id="+student_id+"");
				flag = 1;
			}
			if(flag == 0){
				rs = c.getResult("select staff_id from college_staff where email_id='"+email_id+"'");
				while(rs.next()){
					int staff_id = rs.getInt("staff_id");
					c.dml("delete from groups_member where staff_member_id="+staff_id+"");
					//flag = 1;
				}
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	public int groupWallPostMo(String group_wall_post){
		Connect c = new Connect();
		HttpSession sessioncheck = ServletActionContext.getRequest().getSession();
		HttpSession session = ServletActionContext.getRequest().getSession();
		String username=ServletActionContext.getRequest().getSession().getAttribute("username").toString();
		int user_id = getUserIdMo(username);
		int group_update_id=0;
		try{
			//ResultSet rs14;
			Date date = new Date();
			Timestamp timestamp = new Timestamp(date.getTime());
			System.out.println(timestamp);
			c.dml("insert into groups_update( `group_id`, `updater_id`, `parent_id`, `update`, `updater_role`, `status`, `update_time`) values("+session.getAttribute("group_id")+", "+user_id+", 0, '"+group_wall_post+"', '"+sessioncheck.getAttribute("user_type")+"', 1, '"+timestamp+"')");
			ResultSet rs = c.getResult("select group_update_id from groups_update where `update`='"+group_wall_post+"' and group_update_id = (select max(group_update_id) from groups_update where updater_id="+user_id+" and parent_id=0)");
			while(rs.next()){
				group_update_id = rs.getInt("group_update_id");
			}
			return group_update_id;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return group_update_id;
	}
}
